package com.ColonyCount.Activity;

import java.util.List;

import com.ColonyCount.ColonyCountContext;
import com.ColonyCount.ColonyCountHeap;
import com.ColonyCount.R;
import com.ColonyCount.Image.ImageRegion;
import com.ColonyCount.View.CountHistogramView;
import com.ColonyCount.View.RegionView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ResultActivity extends Activity {

	ColonyCountContext cccontext;
	RegionView regionsView;
	TextView countValue;
	TextView meanSizeValue;
	TextView minSizeValue;
//    CountHistogramView countHistrogram;
    int minRegionSize = 30;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
    	ColonyCountContext.dumpActivityEvent(this, "ResultActivity.onCreate() start");

		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);
		
		cccontext = ColonyCountContext.getSingleton();
		regionsView = (RegionView) findViewById(R.id.imagePreview);
		countValue = (TextView) findViewById(R.id.countValue);
		meanSizeValue = (TextView) findViewById(R.id.meanSizeValue);
		minSizeValue = (TextView) findViewById(R.id.minSizeValue);
			
//		countHistrogram = (CountHistogramView) findViewById(R.id.histogram);
		
		
		
		Button buttonThreshold = (Button) findViewById(R.id.buttonMainMenu);
		buttonThreshold.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent myIntent = new Intent(ResultActivity.this, MenuActivity.class);
		        startActivity(myIntent);
			}
		});
		
    	ColonyCountContext.dumpActivityEvent(this, "ResultActivity.onCreate() end");
	}
    
    @Override
	protected void onResume() {
    	super.onResume();
    	
		regionsView.setSourceBitmap(ColonyCountHeap.getLumaBitmap());
		
		List<ImageRegion> regions = ColonyCountHeap.getRegions();
		regionsView.setRegions(regions);
		
//		int maxSize = 0;
//		for(ImageRegion region : regions)
//		{
//			maxSize = Math.max(region.getSize(), maxSize);
//		}
//		int[] values = new int[maxSize + 1];
//		int maxCount = 0;
//		for(ImageRegion region : regions)
//		{
//			int size = region.getSize();
//			values[size] = values[size] + 1;
//			maxCount = Math.max(values[size], maxCount);
//		}
//		countHistrogram.setValues(values, maxCount);
		
		updateMinSize(minRegionSize);
    }
	
    void updateMinSize(int minSize)
    {
    	regionsView.setMinRegionSize(minSize);
    	
    	int count = 0;
    	int sizeSum = 0;
    	List<ImageRegion> regions = ColonyCountHeap.getRegions();
    	for(ImageRegion region : regions)
    	{
    		if(region.getSize() >= minSize)
    		{
    			count ++;
    		}
    		sizeSum += region.getSize();
    	}
    	meanSizeValue.setText("" + sizeSum/regions.size());
    	minSizeValue.setText("" + minSize);
    	countValue.setText("" + count);
    }
    
    
    @Override
	protected void onPause() {
		super.onPause();
		regionsView.setSourceBitmap(null);
	}
}
